package com.sevennotes.classtable

import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.toArgb
import androidx.lifecycle.viewModelScope
import com.sevennotes.classtable.config.Settings
import com.sevennotes.classtable.model.Time
import com.sevennotes.classtable.utils.CommonUtils
import com.sevennotes.classtable.utils.TimeUtil
import com.sevennotes.classtable.utils.WebdavTool
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking
import org.junit.Test
import java.io.BufferedReader
import java.io.InputStreamReader
import java.lang.StringBuilder
import java.util.*

/**
 * Example local unit test, which will execute on the development machine (host).
 *
 * See [testing documentation](http://d.android.com/tools/testing).
 */
class ExampleUnitTest {
  @Test
  fun test() {
    print(
      TimeUtil.conversionTimeToString(
        Time(10, 5),
        40,
        3,
        false
      )
    )
  }

  @Test
  fun colorTest() {
    val a = Color.Green
    val b = a.value
    val c = b.toLong()
    val d = Color(c)
    println(a.value)
    println(d.value)
  }

  @Test
  fun calendartest() {
    val c = Date()
    val c1 = Calendar.getInstance()
    c1.set(Calendar.HOUR_OF_DAY, 23)
    c1.set(Calendar.MINUTE, 59)
    c1.set(Calendar.SECOND, 59)
    println(c1.time.toString())
  }

  fun initWebdav() {
    WebdavTool.initSardine(
      userName = "846226724@qq.com",
      password = "aawxxdjqn3w6e4f8",
      url = "https://dav.jianguoyun.com/dav/"
    )
  }

  @Test
  fun testReadPaths() {
    initWebdav()
    val list = WebdavTool.getExistFiles("ceshi")
    list.forEach {
      println("got a name: $it")
    }
  }

  @Test
  fun createDir() {
    initWebdav()
    runBlocking {
      WebdavTool.createDirSafely("ceshi4/auto/abc/exd")
    }
  }

  @Test
  fun testAutoSaveFileName() {
    val name = CommonUtils.generateFileNameWithDate("auto_", "json")
    println("name: $name")
  }

  @Test
  fun testGetFiles() {
    runBlocking(Dispatchers.IO) {
      initWebdav()
      val existFileList = WebdavTool.getExistFiles(Settings.autoSaveDir)
      if (existFileList.size > Settings.maxAutoSaveFileNumber) {
        //文件数量超过最大值，需要清理
        existFileList
          .sortedBy { it.modified }
          .subList(0, existFileList.size - Settings.maxAutoSaveFileNumber)
          .forEach { fileRes ->
            WebdavTool.deleteFile("${Settings.autoSaveDir}/${fileRes.name}")
          }
      }
    }
  }

  @Test
  fun jianguoTest() {
    initWebdav()
//    val s = "我是一段非常牛逼的文字哈哈行啊"
//    WebdavTool.uploadByteArray(
//      fileName = "ceshi.txt",
//      dirPath = "ceshi",
//      data = s.toByteArray()
//    )
    val ins = WebdavTool.readFile("ceshi/ceshi.txt")
    val result = BufferedReader(InputStreamReader(ins)).useLines { l ->
      val r = StringBuilder()
      l.forEach {
        r.append(it)
      }
      r.toString()
    }
    println(result)
  }

  @Test
  fun testColor() {
    val color1 = Color(0x0, 0xBC, 0xD4, 0xFF)
    val argb = color1.toArgb()
    val color2 = Color(argb)
    val argb2 = color2.toArgb()
    println("argb1: $argb   argb2: $argb2")
  }
}